﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace Lamex.Library
{
    public partial class frmSelectExport :Lamex.Library.frmShow
    {
        GridView view = new GridView();
        string path = "";
        public frmSelectExport(GridView  view,string path)
        {
            InitializeComponent();
            this.view = view;
            this.path = path;
            this.Load += new EventHandler(frmSelectExport_Load);
        }
        private void frmSelectExport_Load(object sender,EventArgs e)
        {
            ucImportType.Properties.Items.Add("导出数据");
            ucImportType.Properties.Items.Add("导出格式");
            ucImportType.SelectIndex = 0;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if(ucImportType.Text== "导出数据")
                {
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.Title = "导出Excel";
                    fileDialog.InitialDirectory = path;//导出默认路径
                    fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";//导出默认格式
                    fileDialog.FileName = "MES_TEMP_" + DateTime.Now.ToString("yyMMdd_hhMMss");//导出默认名称
                    DialogResult dialogResult = fileDialog.ShowDialog(this);
                    if (dialogResult == DialogResult.OK)
                    {
                        DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                        // gridControl1.Font = new Font("宋体", 10);
                        view.ExportToXlsx(fileDialog.FileName);
                        //DevExpress.XtraEditors.XtraMessageBox.Show("导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (DevExpress.XtraEditors.XtraMessageBox.Show("导出成功，是否打开文件？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(fileDialog.FileName);//打开指定路径下的文件
                    }
                    this.Close();
                }
                else
                {
                    SaveFileDialog fileDialog = new SaveFileDialog();
                    fileDialog.Title = "导出Excel";
                    fileDialog.InitialDirectory = path;//导出默认路径
                    fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";//导出默认格式
                    fileDialog.FileName = "MES_TEMP_" + DateTime.Now.ToString("yyMMdd_hhMMss");//导出默认名称
                    DialogResult dialogResult = fileDialog.ShowDialog(this);
                    if (dialogResult == DialogResult.OK)
                    {
                        DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                        // gridControl1.Font = new Font("宋体", 10);
                        Lamex.Common.ExecelHelper.ExportToExcelHeader(view, fileDialog.FileName);
                        //DevExpress.XtraEditors.XtraMessageBox.Show("导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (DevExpress.XtraEditors.XtraMessageBox.Show("导出成功，是否打开文件？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(fileDialog.FileName);//打开指定路径下的文件
                    }
                    this.Close();
                }
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
    }
}